home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / DriverManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  6.1 KB  |  231 lines

  1. package symjava.sql;
  2.  
  3. import java.io.PrintStream;
  4. import java.util.Enumeration;
  5. import java.util.Hashtable;
  6. import java.util.Properties;
  7. import java.util.Vector;
  8.  
  9. public class DriverManager {
  10.    private static Vector drivers = new Vector();
  11.    private static int loginTimeout;
  12.    private static PrintStream logStream = null;
  13.    private static boolean initialized;
  14.  
  15.    public static synchronized Connection getConnection(String url, Properties info) throws SQLException {
  16.       if (url == null) {
  17.          throw new SQLException("The url cannot be null", "08001");
  18.       } else {
  19.          println("DriverManager.getConnection(\"" + url + "\")");
  20.          if (!initialized) {
  21.             initialize();
  22.          }
  23.  
  24.          Object currentSecurityContext = getSecurityContext();
  25.          SQLException reason = null;
  26.  
  27.          for(int i = 0; i < drivers.size(); ++i) {
  28.             DriverInfo di = (DriverInfo)drivers.elementAt(i);
  29.             if (di.securityContext != null && di.securityContext != currentSecurityContext) {
  30.                println("    skipping: " + di);
  31.             } else {
  32.                try {
  33.                   println("    trying " + di);
  34.                   Connection result = di.driver.connect(url, info);
  35.                   if (result != null) {
  36.                      println("getConnection returning " + di);
  37.                      return result;
  38.                   }
  39.                } catch (SQLException ex) {
  40.                   if (reason == null) {
  41.                      reason = ex;
  42.                   }
  43.                }
  44.             }
  45.          }
  46.  
  47.          if (reason != null) {
  48.             println("getConnection failed: " + reason);
  49.             throw reason;
  50.          } else {
  51.             println("getConnection: no suitable driver");
  52.             throw new SQLException("No suitable driver", "08001");
  53.          }
  54.       }
  55.    }
  56.  
  57.    public static synchronized Connection getConnection(String url, String user, String password) throws SQLException {
  58.       Properties info = new Properties();
  59.       if (user != null) {
  60.          ((Hashtable)info).put("user", user);
  61.       }
  62.  
  63.       if (password != null) {
  64.          ((Hashtable)info).put("password", password);
  65.       }
  66.  
  67.       return getConnection(url, info);
  68.    }
  69.  
  70.    public static synchronized Connection getConnection(String url) throws SQLException {
  71.       Properties info = new Properties();
  72.       return getConnection(url, info);
  73.    }
  74.  
  75.    public static Driver getDriver(String url) throws SQLException {
  76.       println("DriverManager.getDriver(\"" + url + "\")");
  77.       if (!initialized) {
  78.          initialize();
  79.       }
  80.  
  81.       Object currentSecurityContext = getSecurityContext();
  82.  
  83.       for(int i = 0; i < drivers.size(); ++i) {
  84.          DriverInfo di = (DriverInfo)drivers.elementAt(i);
  85.          if (di.securityContext != null && di.securityContext != currentSecurityContext) {
  86.             println("    skipping: " + di);
  87.          } else {
  88.             try {
  89.                println("    trying " + di);
  90.                if (di.driver.acceptsURL(url)) {
  91.                   println("getDriver returning " + di);
  92.                   return di.driver;
  93.                }
  94.             } catch (SQLException var4) {
  95.             }
  96.          }
  97.       }
  98.  
  99.       println("getDriver: no suitable driver");
  100.       throw new SQLException("No suitable driver", "08001");
  101.    }
  102.  
  103.    public static synchronized void registerDriver(Driver driver) throws SQLException {
  104.       if (!initialized) {
  105.          initialize();
  106.       }
  107.  
  108.       DriverInfo di = new DriverInfo();
  109.       di.driver = driver;
  110.       di.className = driver.getClass().getName();
  111.       di.securityContext = getSecurityContext();
  112.       drivers.addElement(di);
  113.       println("registerDriver: " + di);
  114.    }
  115.  
  116.    public static void deregisterDriver(Driver driver) throws SQLException {
  117.       Object currentSecurityContext = getSecurityContext();
  118.       println("DriverManager.deregisterDriver: " + driver);
  119.       DriverInfo di = null;
  120.  
  121.       int i;
  122.       for(i = 0; i < drivers.size(); ++i) {
  123.          di = (DriverInfo)drivers.elementAt(i);
  124.          if (di.driver == driver) {
  125.             break;
  126.          }
  127.       }
  128.  
  129.       if (i >= drivers.size()) {
  130.          println("    couldn't find driver to unload");
  131.       } else if (currentSecurityContext != null && di.securityContext != currentSecurityContext) {
  132.          throw new SecurityException();
  133.       } else {
  134.          drivers.removeElementAt(i);
  135.       }
  136.    }
  137.  
  138.    public static Enumeration getDrivers() {
  139.       Vector result = new Vector();
  140.       if (!initialized) {
  141.          initialize();
  142.       }
  143.  
  144.       Object currentSecurityContext = getSecurityContext();
  145.  
  146.       for(int i = 0; i < drivers.size(); ++i) {
  147.          DriverInfo di = (DriverInfo)drivers.elementAt(i);
  148.          if (di.securityContext != null && di.securityContext != currentSecurityContext) {
  149.             println("    skipping: " + di);
  150.          } else {
  151.             result.addElement(di.driver);
  152.          }
  153.       }
  154.  
  155.       return result.elements();
  156.    }
  157.  
  158.    public static void setLoginTimeout(int seconds) {
  159.       loginTimeout = seconds;
  160.    }
  161.  
  162.    public static int getLoginTimeout() {
  163.       return loginTimeout;
  164.    }
  165.  
  166.    public static void setLogStream(PrintStream out) {
  167.       logStream = out;
  168.    }
  169.  
  170.    public static PrintStream getLogStream() {
  171.       return logStream;
  172.    }
  173.  
  174.    public static void println(String message) {
  175.       if (logStream != null) {
  176.          logStream.println(message);
  177.       }
  178.  
  179.    }
  180.  
  181.    private static Object getSecurityContext() {
  182.       SecurityManager security = System.getSecurityManager();
  183.       return security == null ? null : security.getSecurityContext();
  184.    }
  185.  
  186.    private static void loadInitialDrivers() {
  187.       String drivers;
  188.       try {
  189.          drivers = System.getProperty("jdbc.drivers");
  190.       } catch (Exception var5) {
  191.          drivers = null;
  192.       }
  193.  
  194.       println("DriverManager.initialize: jdbc.drivers = " + drivers);
  195.       if (drivers != null) {
  196.          while(drivers.length() != 0) {
  197.             int x = drivers.indexOf(58);
  198.             String driver;
  199.             if (x < 0) {
  200.                driver = drivers;
  201.                drivers = "";
  202.             } else {
  203.                driver = drivers.substring(0, x);
  204.                drivers = drivers.substring(x + 1);
  205.             }
  206.  
  207.             if (driver.length() != 0) {
  208.                try {
  209.                   println("DriverManager.Initialize: loading " + driver);
  210.                   Class.forName(driver);
  211.                } catch (Exception ex) {
  212.                   println("DriverManager.Initialize: load failed: " + ex);
  213.                }
  214.             }
  215.          }
  216.  
  217.       }
  218.    }
  219.  
  220.    static void initialize() {
  221.       if (!initialized) {
  222.          initialized = true;
  223.          loadInitialDrivers();
  224.          println("JDBC DriverManager initialized");
  225.       }
  226.    }
  227.  
  228.    private DriverManager() {
  229.    }
  230. }
  231.